<?php
class App_Tpl_Form_Field_Mediafile extends App_Tpl_Form_Field{

	public function PreparePost($ids, $params)
	{
		if (!is_array($ids)) $ids = array();

		$maxItems = isset($params['maxfiles']) ? (int)$params['maxfiles'] : 1;
		$ids = array_slice($ids, 0, $maxItems);
		return array_unique(array_map('intval', (array)$ids));
	}

	public function PrepareOut($ids, $params)
	{
		$ids = array_unique(array_map('intval', (array)$ids));
		$objs = Mozg_Mapper::Get('Media')->GetList(array('ids' => $ids));
		$result = array();
		foreach ($ids as $id)
		{
			if (!isset($objs[$id])) continue;
			$result[$id] = $objs[$id];
		}

		return $result;
	}

    protected function RenderField()
    {
    	$this->thumbFolder = Mozg_Mapper::Get('Image')->GetImageThumbFolder('mediathumbnail');

    	Mozg::SubAction()->Add('RequiredJS', array($this, 'MediaFileJs'), array(), 'MediaFileField');
    	return $this->render('form/field/_mediafile.phtml');
    }

    public function MediaFileJs()
    {
    	$this->thumbFolder = Mozg_Mapper::Get('Image')->GetImageThumbFolder('mediathumbnail');
        return $this->render('form/field/js/_mediafile.js');
    }
}